package com.keyancompany.zhongbaopingtai.activity;

import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.keyancompany.zhongbaopingtai.Constants;
import com.keyancompany.zhongbaopingtai.R;
import com.keyancompany.zhongbaopingtai.api.UserAPI;
import com.keyancompany.zhongbaopingtai.model.BaseModel;
import com.keyancompany.zhongbaopingtai.model.ComplainingModel;
import com.keyancompany.zhongbaopingtai.model.FindModel;
import com.keyancompany.zhongbaopingtai.utils.IntentUtils;

import butterknife.Bind;
import butterknife.BindString;
import butterknife.OnClick;

public class ComplaintDetailActivity extends BaseActivity {

    @BindString(R.string.complainte_detail_text)
    String title;
    @Bind(R.id.susong_num)
    TextView susongNum;
    @Bind(R.id.dangqian_state)
    TextView dangqianState;
    @Bind(R.id.project_name)
    TextView projectName;
    @Bind(R.id.tousufang)
    TextView tousufang;
    @Bind(R.id.bei_tousufang)
    TextView beiTousufang;
    @Bind(R.id.susong_reason)
    TextView susongReason;
    @Bind(R.id.peichang_money)
    TextView peichangMoney;
    @Bind(R.id.tousu_time)
    TextView tousuTime;
    @Bind(R.id.toolbar)
    Toolbar toolbar;

    private ComplainingModel item;

    @Override
    protected int getContentViewId() {
        return R.layout.activity_complaint_detail;
    }

    @Override
    protected int getMenuId() {
        return 0;
    }

    @Override
    protected void initToolBar() {
        initToolBar(toolbar, title);
    }

    @Override
    protected void setUpViews() {
        item = (ComplainingModel) getIntent().getSerializableExtra(Constants.ITEM);

        projectName.setText(item.getProjectName());
        onReadData(null);

        requestTask();
    }

    public void onReadData(ComplainingModel model) {

        susongNum.setText(getString(R.string.susongbianhao_text, model == null ? "" : model.getShortCode()));
        dangqianState.setText(getString(R.string.dangqianzhuangtai_text, model == null ? "" : model.getProgress()));

        beiTousufang.setText(getString(R.string.beitousufang_text, model == null ? "" : model.getShortCode()));

        susongReason.setText(getString(R.string.stousuliyou_text, model == null ? item.getReason() : model.getReason()));

        peichangMoney.setText(getString(R.string.peichangjine_text, model == null ? "" : model.getPayable()));

        tousuTime.setText(getString(R.string.tousushijian_text, model == null ? "" : model.getCreateTime()));

        projectName.setText(model == null ? item.getProjectName() : model.getProjectName());
        tousufang.setText(getString(R.string.tousufang_text, UserAPI.getCurrentUser().getUsername()));
    }

    @OnClick(R.id.project_name)
    public void projectDetail(View v) {
//        IntentUtils.startActivity(this,ProjectDetailActivity.class);
    }

    public void requestTask() {
        UserAPI.complainDetail(item.getId(), this, getLoading(-1));
    }

    @Override
    public void success(int type, String json) {
        super.success(type, json);
        BaseModel base = validateJson(json);
        if (base.isSuccess()) {
            ComplainingModel model = JSONObject.parseObject(base.data, ComplainingModel.class);
            onReadData(model);
        }
    }

    @Override
    public void failed(String error) {
        super.failed(error);
    }
}
